import time
import pytest
import allure
import TestCase

from Base.InitDriver import init_driver
from Page.CommonPage import CommonPage
from Utils.XiaoMaUtils import XiaoMaUtils


@allure.feature('TestCase_03: 语音搜索状态中返回键功能正常')
@allure.issue(
    'https://www.tapd.cn/56767367/sparrow/tcase/view/1156767367001043363?url_cache_key=17e362af8b12add5e45c5e468a11c15a')
class TestCaseMusic03:
    def setup_class(self):
        self.driver = init_driver()
        # 实例化页面
        self.launcher_index_page = CommonPage(self.driver).get_launcher_index_page()
        self.launcher_app_list_page = CommonPage(self.driver).get_launcher_app_list_page()

        self.music_choice_page = CommonPage(self.driver).get_music_choice_page()
        self.music_index_page = CommonPage(self.driver).get_music_index_page()
        self.music_search_page = CommonPage(self.driver).get_music_search_page()

    def teardown_class(self):
        XiaoMaUtils.close_app('music')
        self.driver.quit()

    @allure.story("step1: 点击首页的搜索图标")
    @pytest.mark.run(order=31)
    def test_music_03_1_click_search(self):
        try:
            self.launcher_index_page.click_app_list()
            self.launcher_app_list_page.click_appname(TestCase.MUSIC)
            if self.music_choice_page.is_display_title():
                self.music_choice_page.click_skip()
            self.music_index_page.click_search()
        except Exception as e:
            raise e
        finally:
            allure.attach(self.driver.get_screenshot_as_png(), XiaoMaUtils.get_current_time(),
                          allure.attachment_type.PNG)

    @allure.story("step2: 点击语音搜索按钮, 判断是否进入收音状态")
    @pytest.mark.run(order=32)
    def test_music_03_2_click_voice_search(self):
        try:
            self.music_search_page.click_voice_search()
            assert self.music_search_page.is_display_recording()
        except Exception as e:
            raise e
        finally:
            allure.attach(self.driver.get_screenshot_as_png(), XiaoMaUtils.get_current_time(),
                          allure.attachment_type.PNG)

    @allure.story("step3: 点击back, 判断是否返回音乐首页")
    @pytest.mark.run(order=33)
    def test_music_03_3_is_display_online_music(self):
        try:
            self.music_search_page.click_back()
            assert self.music_index_page.is_display_online_music()
        except Exception as e:
            raise e
        finally:
            allure.attach(self.driver.get_screenshot_as_png(), XiaoMaUtils.get_current_time(),
                          allure.attachment_type.PNG)


if __name__ == '__main__':
    pass
    # pytest.main("-s testcase_music_03.py")
